package com.sky.utils;

import com.sky.properties.MinioProperties;
import com.sky.result.Result;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Component
@Slf4j
public class MinioUtil {
    @Autowired
    private MinioProperties minioProperties;

    /**
     * 文件上传
     */

    public String upload(MultipartFile file) {
        String endpoint = minioProperties.getEndpoint();
        String accessKeyId = minioProperties.getAccessKeyId();
        String accessKeySecret = minioProperties.getAccessKeySecret();
        String bucketName = minioProperties.getBucketName();
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        // 截取原始文件名的后缀 xxx.jpg
        String s = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 构造新文件名称
        String objectName = UUID.randomUUID() + s;

        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            InputStream inputStream = file.getInputStream();

            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(endpoint)
                            .credentials(accessKeyId, accessKeySecret)
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucketName).object(objectName)
                            .stream(inputStream, file.getSize(), -1)
                            .contentType(MimeTypeUtils.IMAGE_JPEG_VALUE)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // http://localhost:9000/sky-take-out/48f04e9e-de95-478d-b69b-906ec0
        // http://192.168.90.45:9000/sky-take-out/4354c269-2478-406a-8848-f6e5e45d39f6.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20230902%2F%2Fs3%2Faws4_request&X-Amz-Date=20230902T141651Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=9ef6a0d01636e4e888902b5eae2570adb1a7177691148078626232b0dc8c968f
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append(endpoint)
                .append("/")
                .append(bucketName)
                .append("/")
                .append(objectName);
        log.info("文件路径:{}", stringBuilder);
        return stringBuilder.toString();
    }

}
